﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using TTGame;

/*
 * SkillConfigInfo
 *
 *
 *
 * 依赖：
 * 不依赖于其他类
 *
 * ！不写重复代码！
 * @author TTun
 * @date 7/12/2017 5:16:56 PM
 */
namespace TTGameEngine {

public enum ESkillType {
    eST_NormalHandFight = 1,//普通触发攻击
    eST_NormalFly,      //普通子弹攻击
    eST_SingleNoTarget, //单体无目标，非强制命中(治疗 随机打击)
    eST_NormalArea,     //普通群体
    eST_FollowArea,     //跟随群体
    eST_Teleport,       //瞬移
    eST_RunAttack,      //冲撞
}

public enum ELockTagetType {
    eLT_NoTarget = 0,
    eLT_Target,//当前锁定目标
    eLT_Target_Near,//目标选择 根据距离
    eLT_Tagret_Hp,//根据血量
    eLT_Target_Random,//随机
    eLT_Target_Attrubte,//根据属性
    eLT_Self,//锁定自己
    //eLT_HandContro,
}


public enum EConditionType {
    None,
    EnterGameTime,//进入战斗时间
    SelfHp,//自身血量
}
public enum ConditionCompareType : byte {
    none,
    less,
    lessEqual,
    equal,
    unEqual,
    large,
    largeEqual,
}

public class ConditionInfo {
    public EConditionType condition;
    public ConditionCompareType compare;
    public float value;

    public ConditionInfo(EConditionType type, ConditionCompareType pare, float v) {
        condition = type;
        compare = pare;
        value = v;
    }
}

public enum SkillEventType {
    None,
    AudioEvent,
    CameraEvent,
}

[System.Serializable]
public class SkillConfigInfo {
    public string skillName;
    public uint skillID;
    public float cdTime;
    public float range;
    public ESkillType skillType;
    public SkillCampID skillCampType; //锁定阵营
    public ELockTagetType lockType;//锁定阵营的方式
    public int lockPrior;//锁定对象的辅助优先级

    public int damageConfigID;
    public DamageValueID damageValueID;
    public string attackEffect;
    public EffectSetting aESetting;
    public string woundEffect;

    public string prepareAnimName;
    public bool prepareSyn;
    public float prepareTime;
    public string prepareEffect;
    public EffectSetting pESetting;
    public string prepareSound;
    public float pSoundDelay;

    public string releaseAnimName;
    public bool releaseSyn;
    public float releaseTime;
    public string releaseEffect;
    public EffectSetting rESetting;
    public string releaseSound;
    public float rSoundDelay;
    public int releaseAnimCount;//动画播放次数

    public int basicDamage;
    public int damageRatio;//(0-100)
    public float globalCD;
    public int relationSkillID = -1;
    public int[] buffId;

    public string[] attackFrameInfo; // 攻击帧信息
    public string[] releaseCondition;// 技能释放条件

    //处理数据
    private bool initilize;
    private List<ConditionInfo> conditions;

    public void OnInitData() {
        if(!initilize) {
            ParseConditions();

            pESetting.effectName = prepareEffect;
            aESetting.effectName = attackEffect;
            rESetting.effectName = releaseEffect;
            initilize = true;
        }
    }

    //解析释放条件
    public void ParseConditions() {
        if(releaseCondition != null && releaseCondition.Length > 0) {
            ConditionCompareType compare = ConditionCompareType.none;
            float v = 0f;
            ConditionInfo info = null;
            conditions = new List<ConditionInfo>();
            for(int i = 0; i < releaseCondition.Length; i++) {
                string signal = releaseCondition[i].Trim().ToLower();
                EConditionType conditionType = EConditionType.None;
                #region Condition create
                if(signal.StartsWith("entergame")) {
                    signal = signal.Substring(9);
                    conditionType = EConditionType.EnterGameTime;
                } else if(signal.StartsWith("selfhp")) {
                    signal = signal.Substring(6);
                    conditionType = EConditionType.SelfHp;
                }
                #endregion
                if(conditionType != EConditionType.None) {
                    compare = GetCompareType(ref signal);
                    v = float.Parse(signal);
                    info = new ConditionInfo(EConditionType.EnterGameTime, compare, v);
                    conditions.Add(info);
                } else {
                    DebugUtil.LogError("没有这种类型的条件!!");
                }
            }
        }
    }

    #region Condition Compare
    public ConditionCompareType GetCompareType(ref string signal) {
        ConditionCompareType type = ConditionCompareType.none;
        if(signal.StartsWith("<=")) {
            type = ConditionCompareType.lessEqual;
            signal = signal.Substring(2);
        } else if(signal.StartsWith("<")) {
            type = ConditionCompareType.less;
            signal = signal.Substring(1);
        } else if(signal.StartsWith("=")) {
            type = ConditionCompareType.equal;
            signal = signal.Substring(1);
        } else if(signal.StartsWith("!=")) {
            type = ConditionCompareType.unEqual;
            signal = signal.Substring(2);
        } else if(signal.StartsWith(">=")) {
            type = ConditionCompareType.largeEqual;
            signal = signal.Substring(2);
        } else if(signal.StartsWith(">")) {
            type = ConditionCompareType.large;
            signal = signal.Substring(1);
        } else {
            DebugUtil.LogError("没有这种比较类型！");
        }
        return type;
    }
    public bool Compare(float cValue, ConditionInfo info) {
        if(info.compare == ConditionCompareType.lessEqual) {
            return cValue <= info.value;
        } else if(info.compare == ConditionCompareType.less) {
            return cValue < info.value;
        } else if(info.compare == ConditionCompareType.large) {
            return cValue > info.value;
        } else if(info.compare == ConditionCompareType.largeEqual) {
            return cValue >= info.value;
        } else if(info.compare == ConditionCompareType.equal) {
            return cValue == info.value;
        } else if(info.compare == ConditionCompareType.unEqual) {
            return cValue != info.value;
        }
        return false;
    }
    public bool CheckCondition(Puppet obj) {
        if(conditions == null || conditions.Count == 0) {
            return true;
        }
        for(int i = 0; i < conditions.Count; i++) {
            ConditionInfo signalInfo = conditions[i];
            bool flag = false;
            float cValue;
            switch(signalInfo.condition) {
                case EConditionType.EnterGameTime:
                    cValue = obj.m_puppetAI.m_fightActiveTime;
                    flag = Compare(cValue, signalInfo);
                    break;
                case EConditionType.SelfHp:
                    cValue = ((float)obj.CurHp) / obj.MaxHp;
                    flag = Compare(cValue, signalInfo);
                    break;
                default: DebugUtil.LogError("位置条件！"); break;
            }
            if(!flag) { return false; }
        }
        return true;
    }
    #endregion

}


#region EffectSetting
public enum DirectionType {
    None,
    Forward,
}

//特效的设置方式
[System.Serializable]
public class EffectSetting {

    public PuppetPointType pointType;//挂靠节点类型
    public bool parentFlag = true;  //是否设置父节点
    public DirectionType dirType;//方向类型
    public float delayTime; //延迟时间

    [HideInInspector]
    public string effectName;
    [HideInInspector]
    public float curentDelayTime;//记录当前延迟时间
}

public class SkillAudioSetting {
    public string audioName;
    public float delayTime;

    public SkillAudioSetting(string name, float delay) {
        audioName = name;
        delayTime = delay;
    }
}

#endregion

}